import 'package:amoy_ticket/home/home_tabBar_one.dart';
import 'package:amoy_ticket/home/home_tabbar_eight.dart';
import 'package:amoy_ticket/home/home_tabbar_five.dart';
import 'package:amoy_ticket/home/home_tabbar_four.dart';
import 'package:amoy_ticket/home/home_tabbar_nine.dart';
import 'package:amoy_ticket/home/home_tabbar_six.dart';
import 'package:amoy_ticket/home/home_tabbar_three.dart';
import 'package:amoy_ticket/utils/router/router_util.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'home_tabbar_seven.dart';

class HomeMainPage extends StatefulWidget {
  HomeMainPage({Key? key}) : super(key: key);

  @override
  _HomeMainPageState createState() => _HomeMainPageState();
}

class _HomeMainPageState extends State<HomeMainPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  Color _startBackground = Color(0xFFfbe0e6);
  Color _endBackground = Color(0xFFfff8f8);
  Color _tabTextColor = Colors.black;
  int _today = 0;
  String citySelect = '南京';
  List _tabs = [
    '精选',
    '快看',
    '关于我妈的一切',
    '周末看啥',
    '最新预告',
    '口碑趋势',
    '最新影讯',
    '演唱会',
    '话剧歌剧',
  ];

  @override
  void initState() {
    super.initState();
    DateTime dateTime = DateTime.now();
    _today = dateTime.day;

    _tabController = TabController(length: _tabs.length, vsync: this);
    _tabController.addListener(() {
      if (_tabController.index == 0) {
        _startBackground = Color(0xFFfbe0e6);
        _endBackground = Color(0xFFfff8f8);
        _tabTextColor = Colors.black;
      } else if (_tabController.index == 4) {
        _startBackground = Color(0xFFf1d53b3);
        _endBackground = Color(0xFF2b358e);
        _tabTextColor = Colors.white;
      } else {
        _startBackground = Color(0xFFfbe0e6);
        _endBackground = Color(0xFFfff8f8);
        _tabTextColor = Colors.black;
      }
      setState(() {});
    });
  }

  Widget _tabBar() {
    return TabBar(
      controller: _tabController,
      isScrollable: true,
      labelColor: _tabTextColor,
      unselectedLabelColor: _tabTextColor,
      indicatorColor: _tabTextColor,
      labelStyle: TextStyle(fontSize: 13.sp, fontWeight: FontWeight.bold),
      unselectedLabelStyle: TextStyle(fontSize: 12.sp),
      indicatorSize: TabBarIndicatorSize.label,
      tabs: _tabs.map((e) {
        return Tab(
          text: e,
        );
      }).toList(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return [
            SliverAppBar(
              automaticallyImplyLeading: false,
              // expandedHeight: 300.0,
              elevation: 0,
              toolbarHeight: 90.h,
              pinned: true,
              flexibleSpace: Container(
                padding: EdgeInsets.only(top: 8.h),
                child: Column(
                  children: [_row(), _tabBar()],
                ),
                decoration: BoxDecoration(
                    // color: Color(0xFF0b0d5a),
                    gradient: LinearGradient(
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                        colors: [
                      _startBackground,
                      _endBackground,
                    ])),
              ),
              // backgroundColor: Color(0xFF0b0d5a),
            ),
          ];
        },
        body: TabBarView(
          controller: _tabController,
          children: [
            HomeTabBarOne(),
            // _homeBarOne(),
            CachedNetworkImage(
              imageUrl:
                  'https://upload-images.jianshu.io/upload_images/7439651-d5e585fa4baa1e5f.gif?imageMogr2/auto-orient/strip|imageView2/2/w/298/format/webp',
            ),
            HomeTabBarThree(),
            HomeTabBarFour(),
            HomeTabBarFive(),
            HomeTabBarSix(),
            HomeTabBarSeven(),
            HomeTabBarEight(),
            HomeTabBarNine(),
          ],
        ),
      ),
    );
  }

  Widget _row() {
    return Row(
      children: [
        GestureDetector(
          child: Container(
            width: 26.w,
            child: Center(
              child: Text(
                '证照信息',
                style: TextStyle(
                    // backgroundColor: Color(0xFFfff8f8),
                    color: _tabTextColor,
                    fontSize: 10.sp),
              ),
            ),
          ),
          onTap: () {
            RouterUtil.navigateTo(context, LICENSE_INFO);
          },
        ),
        SizedBox(width: 4.w),
        GestureDetector(
          //TODO: 城市选择器

          onDoubleTap: () {
            citySelect = '南京';
            setState(() {});
          },
          child: Row(
            children: [
              Text(
                citySelect,
                style: TextStyle(
                    color: _tabTextColor,
                    fontSize: 13.sp,
                    fontWeight: FontWeight.bold),
              ),
              Icon(
                Icons.arrow_drop_down,
                color: _tabTextColor,
                size: 20.r,
              )
            ],
          ),
        ),
        Expanded(
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 10.w),
            padding: EdgeInsets.symmetric(horizontal: 5.w, vertical: 5.h),
            // height: 30.h,
            decoration: BoxDecoration(
              color: Color(0xFFffffff),
              borderRadius: BorderRadius.all(
                Radius.circular(20.r),
              ),
            ),
            child: Row(
              children: [
                Icon(
                  Icons.search,
                  size: 16.r,
                  color: Color(0xFFa0a2c9),
                ),
                Expanded(
                    child: Text(
                  '搜影片、影院、影人、演出、资讯、视频',
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                  style: TextStyle(
                      color: Color(0xFFa0a2c9),
                      fontSize: 12.sp,
                      fontWeight: FontWeight.bold),
                ))
              ],
            ),
          ),
        ),
        GestureDetector(
          child: Image.asset(
            'assets/images/scan_icon.png',
            width: 24.w,
            height: 24.w,
            color: _tabTextColor,
          ),
        ),
        SizedBox(width: 15),
        Stack(
          children: [
            Image.asset(
              'assets/images/calendar_bg.png',
              width: 25.w,
              height: 25.w,
              fit: BoxFit.fill,
              color: _tabTextColor,
            ),
            Container(
              //当数字小于10的时候 left为12,大于10为8
              padding: EdgeInsets.only(top: 8.h, left: _today < 10 ? 9.w : 6.w),
              child: Center(
                child: Text(
                  _today.toString(),
                  style: TextStyle(color: _tabTextColor, fontSize: 12.sp),
                ),
              ),
            )
          ],
        ),
        SizedBox(width: 10.w),
      ],
    );
  }
}
